<?php


namespace app\api\services;


use app\admin\model\HtnsProduct;

class OrderCartInfoService
{
    /**
     * @desc 获取订单商品详细信息
     * @param array $orderCartInfo 订单购物车详情
     * @param array $order 订单信息 [coupon_price:优惠券金额,deduction_price:抵扣金额]
     */
    public function getProductDetail($orderCartInfo = [], $order = [])
    {
        $ret['true_sum_price'] = 0;
        $productsArr           = [];

        $attrInfo      = $orderCartInfo['attrInfo'];
        $productsArr[] = [
            'id'        => $orderCartInfo['product_id'],
            'sum_price' => $orderCartInfo['cart_num'] * $orderCartInfo['truePrice'],
        ];
        $discount = 0;$discountArr = [];
//        if($order['paid']){
//            $discount    = $order['total_price'] - $order['pay_price'];// 订单金额-实付金额
//            $discountArr = $this->productDiscountPrice($productsArr, $discount , $order['total_price']);
//        }
        $htnsProduct = new HtnsProduct();
        $productInfo = $orderCartInfo['productInfo'];
        $attrInfo    = $orderCartInfo['attrInfo'];

        $product['id']         = $orderCartInfo['product_id'];
        $erp_id = isset($productInfo['erp_id']) ? $productInfo['erp_id'] : 1;
        $product['hcname']         = $htnsProduct->hcName[$erp_id];
        $product['store_name'] = $productInfo['store_name'];
        $product['img'] = $attrInfo['image'];
        $product['price']      = $orderCartInfo['truePrice'];
        $product['cart_num']   = $orderCartInfo['cart_num'];
        $product['sum_price']  = $orderCartInfo['cart_num'] * $product['price'];

        $product['discount']      = 0;// 优惠金额
        $product['discount_rate'] = 0;// 优惠金额比例
        foreach ($discountArr as $vd) {
            if ($vd['id'] == $orderCartInfo['product_id']) {
                $product['discount']      = $vd['discount'];
                $product['discount_rate'] = $vd['rate'];
            }
        }

        $product['true_sum_price'] = bcsub($product['price'] * $product['cart_num'],$product['discount'],2);// 优惠金额
        $ret['true_sum_price']     += $product['true_sum_price'];
        $ret['products'] = $product;
        return $ret;
    }
    // 商品优惠金额计算

    /**
     * @desc 商品优惠金额计算
     * @param array $products [id:商品id , sum_price:商品总价]
     * @param string $discount 优惠总金额
     * @return array
     */
    public function productDiscountPrice($products = [], $discount = '',$orderTotalPrice = 0)
    {
        $sum = array_sum(array_column($products, 'sum_price'));
        if($orderTotalPrice){
            $sum = $orderTotalPrice;
        }
        foreach ($products as $k => $v) {
            $discountv = 0;
            $rate = 0;
            if($discount){
                $rate                     = bcdiv($v['sum_price'], $sum, 3);// 所占比例
                $rate = round($rate , 2);
                $discountv                 = bcmul($rate , $discount , 3);// 优惠金额
                $discountv = round($discountv , 2);
            }
            $products[$k]['discount'] = $discountv;
            $products[$k]['rate']     = $rate;
        }
        return $products;
    }
}